package com.systemic.service.impl;

import com.systemic.mapper.MindfulnessCollectMapper;
import com.systemic.mapper.MindfulnessMediaMapper;
import com.systemic.pojo.MindfulnessCollect;
import com.systemic.pojo.MindfulnessMedia;
import com.systemic.service.MindfulnessCollectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: MindfulnessCollectServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-02-02 08:54
 * @Version:
 **/
@Service
public class MindfulnessCollectServiceImpl implements MindfulnessCollectService {

    @Resource
    private MindfulnessCollectMapper mindfulnessCollectMapper;

    @Resource
    private MindfulnessMediaMapper mindfulnessMediaMapper;

    /**
     * 收藏
     * @param mindfulnessCollect
     * @return
     */
    @Override
    @Transactional
    public int add(MindfulnessCollect mindfulnessCollect) {
        int row = 0;
        mindfulnessCollect.setGmtCreate(new Date());
        // TODO:新增收藏量
        MindfulnessMedia media = mindfulnessMediaMapper.selectById(mindfulnessCollect.getMid());
        if (media.getCollect()==null){
            media.setCollect(1);
        } else if(media.getCollect()==0){
            media.setCollect(1);
        }else{
            media.setCollect(media.getCollect()+1);
        }
        row = mindfulnessMediaMapper.update(media)+row;
        row = mindfulnessCollectMapper.insert(mindfulnessCollect)+row;
        return row;
    }

    @Override
    public List<MindfulnessMedia> selectByUid(Integer uid) {
        MindfulnessCollect collect = new MindfulnessCollect();
        collect.setUid(uid);
        List<MindfulnessCollect> mindfulnessCollectList = mindfulnessCollectMapper.select(collect);
        List<MindfulnessMedia> mediaList = new ArrayList<>();
        if (!mindfulnessCollectList.isEmpty()){
            for (MindfulnessCollect mindfulnessCollect:mindfulnessCollectList) {
                MindfulnessMedia mindfulnessMedia = mindfulnessMediaMapper.selectById(mindfulnessCollect.getMid());
                mediaList.add(mindfulnessMedia);
            }
        }
        return mediaList;
    }

    @Override
    @Transactional
    public int delete(Integer id) {
        MindfulnessCollect mindfulnessCollect = new MindfulnessCollect();
        mindfulnessCollect.setId(id);
        MindfulnessCollect collect = mindfulnessCollectMapper.selectONE(mindfulnessCollect);
        int row = 0;
        // TODO:新增收藏量
        MindfulnessMedia media = mindfulnessMediaMapper.selectById(collect.getMid());
        if (media.getCollect()==null){
            media.setCollect(0);
        } else if(media.getCollect()==0){
            media.setCollect(0);
        }else{
            media.setCollect(media.getCollect()-1);
        }
        row = mindfulnessMediaMapper.update(media)+row;
        row = mindfulnessCollectMapper.delete(mindfulnessCollect)+row;
        return row;
    }
}
